package com.cbw.firegame.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cbw.common.utils.R;
import com.cbw.common.vo.CategoryEntity;
import com.cbw.firegame.product.entity.CategoryGameRelationEntity;
import com.cbw.firegame.product.service.CategoryGameRelationService;
import com.cbw.firegame.product.service.CategoryService;
import com.cbw.firegame.product.service.GameService;
import com.cbw.firegame.product.vo.StatisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("product/statistic")
public class StatisticController {


    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryGameRelationService  categoryGameRelationService;

    @Autowired
    private GameService gameService;

    @RequestMapping("/category")
    public R statisticCategory(){
        StatisticVo statisticVo = new StatisticVo();
        List<CategoryEntity> categorys = categoryService.list();
        List<String> collect = categorys.stream().map(item -> item.getCatName()).collect(Collectors.toList());
        statisticVo.setCategory(collect);

        List<Integer> gameNums = new ArrayList<>();
        categorys.forEach(item->{
            int gameNum = categoryGameRelationService.count(new QueryWrapper<CategoryGameRelationEntity>().eq("cat_id", item.getCatId()));
            gameNums.add(gameNum);
        });
        statisticVo.setGameNum(gameNums);
        return R.ok().setData(statisticVo);
    }

    @RequestMapping("/Allgame")
    public R statisticAllgame(){
        int count = gameService.count();
        return R.ok().setData(count);
    }
    @RequestMapping("/salegame")
    public R statisticSalegame(){
      int count=gameService.statisticSalegame();
        return R.ok().setData(count);
    }

}
